#################################################################################
### Racialized Misinformation, Factual Corrections, and Prejudicial Attitudes ###
### [Demographic Information for Study 2]                                     ###
### Authors: Eddy S. F. Yeung, Joseph Glasgow                                 ###
### Date: June 26, 2025                                                       ###
#################################################################################

### Set-up ----
## Clean the working environment and set the working directory
rm(list = ls())
setwd("~/Desktop/racialized_misinfo/replication/Study 2") # set your working directory here, which should also contain the survey data ("study2_dataset.csv")

## Import the dataset
df <- read.csv("study2_dataset.csv")

## Load the required packages
library(tidyverse)

### Sample demographics ----
## Sex
df$sex <- factor(df$sex, levels = 1:3, labels = c("Male", "Female", "Nonbinary"))
table(df$sex) %>% prop.table() * 100

## Age
df$age <- df$yob + 2023 - 2006
df <- df %>% mutate(age6 = case_when(
  age >= 18 & age <= 24 ~ 1,
  age >= 25 & age <= 34 ~ 2,
  age >= 35 & age <= 44 ~ 3,
  age >= 45 & age <= 54 ~ 4,
  age >= 55 & age <= 64 ~ 5,
  age >= 65             ~ 6
))
df$age6 <- factor(df$age6, levels = 1:6, 
                  labels = c("18-24", "25-34", "35-44", "45-54", "55-64", "65+"))
table(df$age6) %>% prop.table() * 100

## Race
# Separate the comma-separated values into multiple rows
df_long <- df %>%
  mutate(race = strsplit(as.character(race), ",")) %>%
  unnest(race)

# Create a column indicating the presence of each race category
df_long <- df_long %>%
  mutate(present = 1)

# Use pivot_wider to reshape the data
df <- df_long %>%
  pivot_wider(names_from = race, names_prefix = "race_", values_from = present, values_fill = 0)

# Rename the race variables
df <- df %>% rename(white = race_1, black = race_2, hispanic = race_3)
table(df$white) %>% prop.table() * 100
table(df$black) %>% prop.table() * 100
table(df$hispanic) %>% prop.table() * 100

## Income (4 categories)
df <- df %>% mutate(inc4 = case_when(
  income >= 1 & income <= 5   ~ 1,
  income >= 6 & income <= 10  ~ 2,
  income >= 11 & income <= 12 ~ 3,
  income == 13 | income == 14 ~ 4
))
table(df$inc4) %>% prop.table() * 100
